#include "jmysql.h"

JMySql::JMySql()
{
    Init();
}

void JMySql::Init()
{
    // 初始化数据库信息
    db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("");//mysql的地址 123.56.143.198
    db.setPort(3306);
    db.setDatabaseName("");//连接的数据库名称
    db.setUserName("");//mysql登录名
    db.setPassword("");//mysql密码

    // 初始化变量
    User.Id = -1;
    User.NickName = "";
    User.Mail = "";
}

bool JMySql::OpenSql()
{
    if(db.open()) {
        db.close();
        return true;
    }
    else {
        db.close();
       return false;
    }
}

// 返回值：true:表示用户已存在，false：表示用户不存在
bool JMySql::IsUserExist(QString SQL)
{
    try {
        if(db.open()) {
            QSqlQuery query(SQL);
            while (query.next()) {
                User.Id = query.value(0).toInt();
                User.NickName =  query.value(1).toString();
                User.Passwd = query.value(2).toString();
                User.Mail = query.value(3).toString();
            }
        }
        db.close();
    }
    catch(...) {
        db.close();
        return false;
    }

    if(User.Id == -1 || User.NickName == "" || User.Mail == "")
        return false;

    return true;
}

int JMySql::GetMaxUserId()
{
    QString SQL = "select MAX(ID) from LJJ_Users";
    int Id = -1;
    try {
        if(db.open()) {
            QSqlQuery query(SQL);
            while (query.next()) {
                Id = query.value(0).toInt();
            }
        }
        db.close();
    }
    catch(...) {
        db.close();
        return -1;
    }

    return Id;
}

bool JMySql::InsertUser(QString SQL)
{
    try {
        if(db.open()) {
            QSqlQuery query(SQL);
            query.exec(SQL);
        }
        db.close();
    }
    catch(...) {
        db.close();
        return false;
    }

    return true;
}

bool JMySql::IsDbSheetExist(QString SQL, QString TabName)
{
    QString Name = "NULL";
    try {
        if(db.open()) {
            QSqlQuery query(SQL);
            while (query.next()) {
                if(query.value(0).toString() == TabName) {
                    Name = query.value(0).toString();
                    break;
                }
            }
        }
        db.close();
    }
    catch(...) {
        db.close();
        return false;
    }

    if(Name == "NULL")
        return false;

    return true;
}

bool JMySql::GetPasswdNote(QString UsrName, QVector<PSDNOTE> &Data)
{
    QString SQL = QString("select * from %1_PasswdNote").arg(UsrName);
    int Id = -1;
    Data.clear();
    try {
        if(db.open()) {
            QSqlQuery query(SQL);
            while (query.next()) {
                PSDNOTE psd;
                 psd.SWName = query.value(0).toString();
                 psd.Account = query.value(1).toString();
                 psd.OtherAccount = query.value(2).toString();
                 psd.PhoneNum = query.value(3).toString();
                 psd.Passwd = query.value(4).toString();
                 psd.PayPasswd = query.value(5).toString();
                 Data.push_back(psd);
            }
        }
        db.close();
    }
    catch(...) {
        db.close();
        return false;
    }

    return true;
}
